/**
 * Created by wxy on 2018/1/10.
 */
;(function () {
    angular.module('common', [])
    //首页
        .service('TimeLineService', [
            '$http',
            'AnswerService',
            function ($http, AnswerService) {
                var me = this;
                me.data = [];
                me.current_page = 1;
                me.get = function (conf) {
                    if (me.pending || me.no_more_data) return;
                    //
                    me.pending = true;

                    conf = conf || {page: me.current_page};
                    $http.post('/laravel54/public/index.php/api/timeLine', conf)
                        .then(function (r) {
                            if (r.data.status) {
                                if (r.data.data.length) {
                                    //console.log(r.data.data);return false;

                                    me.data = me.data.concat(r.data.data);
                                    //console.log(me.data);
                                    /*统计每一条回答的票数*/
                                    me.data = AnswerService.count_vote(me.data);
                                    me.current_page++;
                                } else {
                                    me.no_more_data = true;
                                }
                            }
                            else {
                                console.error('fail');
                            }
                        }, function (e) {
                            console.error('fail');
                        })
                        .finally(function () {
                            me.pending = false;
                        })
                };
                /*在时间线中投票*/
                me.vote = function (conf) {
                    /*调用核心投票功能*/
                    AnswerService.vote(conf)
                        .then(function (r) {
                            /*如果投票成功 就更新AnswerService中的数据*/
                            if (r)
                                AnswerService.update_data(conf.id);
                        })
                };
                me.reset_state = function () {

                    me.no_more_data = false;
                }
            }
        ])

        .controller('HomeController', [
            '$scope',
            'TimeLineService',
            'AnswerService',
            function ($scope, TimeLineService, AnswerService) {
                var $win;

                $scope.TimeLine = TimeLineService;
                /*重置下状态*/
                TimeLineService.reset_state();
                if (TimeLineService.current_page == 1) {
                    TimeLineService.get();
                }

                $win = $(window);
                $win.on('scroll', function () {
                    if ($win.scrollTop() - $(document).height() + $win.height() > -30) {
                        TimeLineService.get();
                    }
                })

                /*
                 * 监控数据
                 * 如果回答数据有变化同时更新其他模块中的回答数据
                 * */
                $scope.$watch(function () {
                    return AnswerService.data;
                }, function (new_data, old_data) {


                    var time_line_data = TimeLineService.data;
                    for (var k in new_data) {
                        /*更新时间线中的 回答数据*/
                        for (var i = 0; i < time_line_data.length; i++) {
                            if (k == time_line_data[i].id) {

                                time_line_data[i]['users'] = new_data[k]['users'];
                            }
                        }
                    }
                    TimeLineService.data = AnswerService.count_vote(TimeLineService.data);

                }, true)

            }
        ])
})();